class Solution:
    def maxOperations(self, nums: list, k: int) -> int:
        numMap = {}
        for i in range(len(nums)):
            if nums[i] not in numMap:
                numMap[nums[i]] = 0
            numMap[nums[i]] += 1
        res = 0
        for num in nums:
            if numMap[num] == 0:
                continue
            numMap[num] -= 1
            diff = k - num
            if diff not in numMap or numMap[diff] == 0:
                continue
            numMap[diff] -= 1
            res += 1
        return res
